﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using static System.Net.Mime.MediaTypeNames;

namespace JocelinItemDemoHttpSecurity
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            program.MethodA();
            program.MethodB();
        }
        public void MethodA()
        {
            string UserCode = "123456";
            string UserName = "zhangsan";

            WebClient webClient = new WebClient();
            HttpClientHandler handler = new HttpClientHandler();
            // 设置代理
            handler.Proxy = webClient.Proxy;
            handler.UseProxy = true;
            var _HttpClient = new HttpClient(handler);
            string url = "路径地址";
            Dictionary<string, string> dic = new Dictionary<string, string>()
                {
                    {"cardno",UserCode},
                    {"name",UserName},
                };

            HttpContent content = new StringContent(System.Text.Json.JsonSerializer.Serialize(dic), Encoding.UTF8, Application.Json);
            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            HttpResponseMessage response = _HttpClient.PostAsync(url, content).Result;

            string userinfo = response.Content.ReadAsStringAsync().Result.ToString();

            //反序列化
            //UserMessage userMessage = JsonConvert.DeserializeObject<UserMessage>(userinfo);

            //if (userMessage.sfjh == "Y")
            //{

            //}
            //else
            //{

            //}
        }

        /// <summary>
        /// 用户返回信息类MethodA
        /// </summary>
        public class UserMessage
        {
            public string errorcode { get; set; }
            public string errormsg { get; set; }
            public string sfjh { get; set; }
            public string khrq { get; set; }
        }

        public async void MethodB()
        {
            // 构建请求 httpclient
            var host = new HostBuilder()
                .ConfigureServices(services =>
                {
                    services.AddHttpClient();
                })
            .Build();
            var _httpClientFactory = host.Services.GetRequiredService<IHttpClientFactory>();
            var httpClient = _httpClientFactory.CreateClient();

            // 请求地址
            const string url = "路径地址";

            // 请求参数
            var request = new Dictionary<string, string>
            {
                {"cardno","" },
                {"name","" },
            };

            var bodyJson = new StringContent(
                    JsonSerializer.Serialize(request),
                    Encoding.UTF8,
                    Application.Json);

            // 发起请求
            var response = await httpClient.PostAsync(url, bodyJson);

            // 是否成功返回
            if (!response.IsSuccessStatusCode)
                throw new HttpRequestException("response code error");

            // 读取返回的body
            using var contentStream = await response.Content.ReadAsStreamAsync();

            // 转为定义的返回实体
            var resEntity = JsonSerializer.DeserializeAsync<ResponseEntity>(contentStream);

            Console.WriteLine(JsonSerializer.Serialize(resEntity));

            Console.ReadKey();
        }

        /// <summary>
        /// 用户返回信息类MethodB
        /// </summary>
        public class ResponseEntity
        {
            public string? Errorcode { get; set; }
            public string? Errormsg { get; set; }
            public string? Sfjh { get; set; }
            public string? Khrq { get; set; }
        }
    }
}
